<?php
include_once 'class/bookLogin.php';

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/22
 * Time: 14:03
 */
function jieshu()
{

    //连接数据库
    $dsn = 'mysql:dbname=books;host=localhost';
    $user = 'root';
    $password = '123456';
    try {
        $db = new PDO($dsn, $user, $password);

        do {
            //输入卡号
            echo "请输入卡号：";
            $phone = trim(fgets(STDIN));

            //输入图书的编号
            echo "请输入图书编号：";
            $bookID = trim(fgets(STDIN));

            $count = 3;
            while ($db->query("select bookid from record where  bookid = '$bookID'")->fetch()) {

                echo "这本书已借出，请重新输入：";
                $bookID = trim(fgets(STDIN));
                echo "\n";
                if ($count-- == 1) {

                    echo "已输入3次，结束办理！\n";
                    echo "返回首页\n";
                    echo "\n";

                    //调用bookLogin类中moshi()方法
                    $login = new bookLogin();
                    $login->moshi();
                    break;
                }
            }

            echo "\n";
            echo "输入y确认借书，任意键重新输入：";
            $re = trim(fgets(STDIN));
            $re = strtolower($re);

        } while ($re != 'y');

        //借书日期
        $borrowTime = date("Y-m-d");
        //还书日期
        $returnTime = date('Y-m-d', strtotime('+7 days'));

        //添加用户信息数组
        $sqlAdd = $db->prepare("insert into record (userid ,bookid,borrowTime,returnTime) values(:userid,:bookid,:borrowTime,:returnTime)");
        $sqlAdd->execute([
            ':userid' => $phone,
            ':bookid' => $bookID,
            ':borrowTime' => $borrowTime,
            ':returnTime' => $returnTime
        ]);

        //获取影响行数
        $row = $sqlAdd->rowCount();

        //返回影响行数
        //如果影响行数大于等于1，则判断成功，否则失败
        if ($row >= 1) {
            echo "添加成功，受影响的行: 1";
            echo "\n";

            //提示借书成功
            echo "借书成功！\n";
            echo "借书到期日期：" . $returnTime . "\n";
        } else {
            echo "添加失败!";
            echo "\n";
            echo "借书失败!";
            echo "\n";
        }
        echo "\n";
    } catch (PDOException $e) {
        echo 'Connection failed:' . $e->getMessage();
    }
    /*
    $db->query("select bookid,bookName,press,price from book WHERE  bookid='$bookID'");

    $bookID=$db->query("select bookid from book WHERE  bookid='$bookID'")->fetch();
    echo "**********图书信息***************\n";
    echo "图书编号：".$bookID;
    echo "\n";
    echo '图书名称：';
    echo "\n";
    echo '出版社：';
    echo "\n";
    echo '价格：';

    echo "\n";
*/
    /*

    if ($bookID == 1) {
        $count = 5;
        echo "书名：php\n";
        echo "出版社：清华大学出版社\n";
        echo "定价：70￥\n";
        echo "产品编号：046156-01\n";
        echo "是否借书？1/0：";
        $jie = (int)fgets(STDIN);
        if ($jie == 1) {
            for ($i = 5; $i > 0; $i--) {
                echo '一共' . $count . '本，还剩余' . $count . "本\n";
                $count--;
                echo '借书成功！还剩余' . $count . "本\n";
                if ($count == 0) {
                    echo "借书失败！剩余0本\n";
                }
                echo "是否继续借书？1/0：";
                $jie2 = fgets(STDIN);
                if ($jie2 == 0) {
                    break;
                }
            }
        } else {
            echo "借书结束\n";
        }
    }
    if ($bookID == 2) {
        $count = 5;
        echo "书名：java\n";
        echo "出版社：清华大学出版社\n";
        echo "定价：70￥\n";
        echo "产品编号：046156-02\n";
        echo "是否借书？1/0：";
        $jie = (int)fgets(STDIN);
        if ($jie == 1) {
            for ($i = 5; $i > 0; $i--) {
                echo '一共' . $count . '本，还剩余' . $count . "本\n";
                $count--;
                echo '借书成功！还剩余' . $count . "本\n";
                if ($count == 0) {
                    echo "借书失败！剩余0本\n";
                }
                echo "是否继续借书？1/0：";
                $jie2 = fgets(STDIN);
                if ($jie2 == 0) {
                    break;
                }
            }
        } else {
            echo "借书结束\n";
        }
    }
    if ($bookID == 3) {
        $count = 3;
        echo "书名：C++\n";
        echo "出版社：清华大学出版社\n";
        echo "定价：70￥\n";
        echo "产品编号：046156-03\n";
        echo "是否借书？1/0：";
        $jie = (int)fgets(STDIN);
        if ($jie == 1) {
            for ($i = 5; $i > 0; $i--) {
                echo '一共' . $count . '本，还剩余' . $count . "本\n";
                $count--;
                echo '借书成功！还剩余' . $count . "本\n";
                if ($count == 0) {
                    echo "借书失败！剩余0本\n";
                }
                echo "是否继续借书？1/0：";
                $jie2 = fgets(STDIN);
                if ($jie2 == 0) {
                    break;
                }
            }
        } else {
            echo "借书结束\n";
        }
    }
    if ($bookID == 4) {
        $count = 2;
        echo "书名：mysql\n";
        echo "出版社：清华大学出版社\n";
        echo "定价：70￥\n";
        echo "产品编号：046156-04\n";
        echo "是否借书？1/0：";
        $jie = (int)fgets(STDIN);
        if ($jie == 1) {
            for ($i = 5; $i > 0; $i--) {
                echo '一共' . $count . '本，还剩余' . $count . "本\n";
                $count--;
                echo '借书成功！还剩余' . $count . "本\n";
                if ($count == 0) {
                    echo "借书失败！剩余0本\n";
                }
                echo "是否继续借书？1/0：";
                $jie2 = fgets(STDIN);
                if ($jie2 == 0) {
                    break;
                }
            }
        } else {
            echo "借书结束\n";
        }
    }
    */
}